package handler

import (
	"github.com/gin-gonic/gin"
	"golang-websocket-chatroom/api/request"
	"golang-websocket-chatroom/interface/dao"
	"golang-websocket-chatroom/interface/models"
	"golang-websocket-chatroom/pkg"
)

func Login(context *gin.Context) {

	var req request.LoginForm
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}

	var login models.Users
	dao.FindLogin(req.Id, &login)

	if login.Id == 0 {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "账号不存在",
			"data": nil,
		})
		return
	}
	if req.Password != login.Password {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  "密码不正确,登录失败",
			"data": nil,
		})
		return
	}
	token, err := pkg.NewJWT("zk2").CreateToken(pkg.CustomClaims{
		ID: login.Id,
	})
	if err != nil {
		return
	}

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "登录成功",
		"data": token,
	})

}
func Show(context *gin.Context) {

	var req request.ShowForm
	if err := context.ShouldBind(&req); err != nil {
		context.JSON(500, gin.H{
			"code": "500",
			"msg":  err.Error(),
			"data": nil,
		})
		return
	}
	var user models.Users
	dao.Shows(&user, &models.Users{
		Id:       req.Id,
		User:     req.User,
		Password: req.Password,
	})

	context.JSON(200, gin.H{
		"code": "200",
		"msg":  "展示",
		"data": req,
	})

}
